//
//  ViewController.m
//  20-HomeWork
//
//  Created by 李昊林 on 16/8/10.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"
#import "Cartoon.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *cartoonArray;
@property (nonatomic, strong) NSString *currentElementName;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
} 
- (IBAction)saxParse:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Cartoon" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    NSXMLParser *xmlPaser = [[NSXMLParser alloc] initWithData:xmlData];
    xmlPaser.delegate = self;
    [xmlPaser parse];
    
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.cartoonArray = [NSMutableArray array];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    NSLog(@"结点头%@", elementName);
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
         Cartoon *cartoon = [Cartoon new];
        [_cartoonArray addObject:cartoon];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (_currentElementName) {
        Cartoon *cartoon = [_cartoonArray lastObject];
        [cartoon setValue:string forKey:_currentElementName];
        NSLog(@"节点的值%@ --- 节点值结束", string);
    }
   
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"结点尾/%@", elementName);
    _currentElementName = nil;
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@",_cartoonArray);
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
